#include "enemys.h"
#include "snake.h"
#include <QPaintEvent>
#include <QPainter>
#include <iostream>
#include  "math.h"

Enemys::Enemys()//难度模式的智能蛇
{
    QRectF rect1(800,200,nodeWidth,nodeHeight);
  snake1.append(rect1);
    addTop1();
    addTop1();
    addTop1();




}
//利用贪心算法来实现电脑蛇的移动
int Enemys::nextgo1(int Hx, int Hy, int Fx, int Fy, QList<QRectF> snake1, QList<QRectF> snake)
{
    int i;
    int min = 999999,p=0;
     //top
    addTop1();
    if(snake1[0].y()<20||snake1[0].intersects(snake1[1]))
    {
        distance1[0]=999999;
    }
    else
    {
    distance1[0] = abs(Fx - Hx ) + abs(Fy - (Hy-nodeHeight));
    }
    for(int j=0;j<snake.length();j++)
    {

        if(snake1[0].intersects(snake[j]))
        {
            distance1[0]=999999;
        }

    }
    snake1.removeFirst();


//down
     addDown1();
    if(snake1[0].y()>1000||snake1[0].intersects(snake1[1]))
    {
        distance1[1]=999999;
    }
    else
    {
    distance1[1] = abs(Fx - Hx ) + abs(Fy - (Hy+nodeHeight));
    }
    for(int j=0;j<snake.length();j++)
    {


         if(snake1[0].intersects(snake[j]))
         {
             distance1[1]=999999;
         }

    }
    snake1.removeFirst();

  //left
    addLeft1();
    if(snake1[0].x()<20||snake1[0].intersects(snake1[1]))
    {
        distance1[2]=999999;
    }

    else
    {
    distance1[2] = abs(Fx -( Hx-nodeWidth) ) + abs(Fy -Hy);
    }
    for(int j=0;j<snake.length();j++)
    {

       if(snake1[0].intersects(snake[j]))
       {
           distance1[2]=999999;
       }

    }
    snake1.removeFirst();

    //right
    addRight1();
    if(snake1[0].x()>1800||snake1[0].intersects(snake1[1]))
    {
        distance1[3]=999999;
    }
    else
    {
    distance1[3] = abs(Fx -( Hx+nodeWidth) ) + abs(Fy -Hy);
    }
    for(int j=0;j<snake.length();j++)
    {

        if(snake1[0].intersects(snake[j]))
        {
            distance1[3]=999999;
        }

    }
    snake1.removeFirst();


    for (i = 0; i < 4; i++)
        {
            if (min > distance1[i])
            {
                min = distance1[i];
                p = i;
            }
        }
    return p;
}


void Enemys::addTop1()
{
    QPointF leftTop;
    QPointF rightBotom;
    leftTop=QPointF(snake1[0].x(),snake1[0].y()-nodeHeight);
    rightBotom=snake1[0].topRight();
    snake1.insert(0,QRectF(leftTop,rightBotom));
}

void Enemys::addDown1()
{
    QPointF leftTop;
    QPointF rightBotom;
    leftTop=snake1[0].bottomLeft();
    rightBotom=snake1[0].bottomRight()+QPointF(0,nodeHeight);
    snake1.insert(0,QRectF(leftTop,rightBotom));
}

void Enemys::addLeft1()
{
    QPointF leftTop;
    QPointF  rightBottom;
    leftTop=snake1[0].topLeft()-QPointF(nodeWidth,0);
    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake1.insert(0,QRectF(leftTop,rightBottom));

}
void Enemys::addRight1()
{
    QPointF leftTop;
    QPointF rightBottom;
    leftTop=snake1[0].topRight();
    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake1.insert(0,QRectF(leftTop,rightBottom));
}
void Enemys::deleteLast1()
{
    //通过在蛇头添加新物块在蛇尾减去物块来实现移动
    snake1.removeLast();
}

